package com.soul.sys.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.IdUtil;
import com.soul.common.constant.CacheConstants;
import com.soul.common.msg.ApiResult;
import com.soul.common.utils.RedisCache;
import com.soul.sys.bean.dto.LoginDto;
import com.soul.sys.bean.dto.RegisterUserDto;
import com.soul.sys.bean.vo.CaptchaImageVo;
import com.soul.sys.bean.vo.LoginVo;
import com.soul.sys.bean.vo.UserInfoVo;
import com.soul.sys.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;


/**
 * 登录接口
 *
 * @author Shi
 */
@RestController
@Api(value="login", tags = "登录相关接口")
public class LoginController {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private UserService userService;
    /**
     * 用户登录
     * @param loginDto
     * @return
     */
    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public ApiResult<LoginVo> login(@RequestBody LoginDto loginDto){
        LoginVo login = userService.login(loginDto);
        return ApiResult.success(login);
    }
    /**
     * 用户登录
     * @param loginDto
     * @return
     */
    @ApiOperation(value = "用户登录获取token，免图形验证码")
    @PostMapping("/jwt/token")
    public ApiResult<LoginVo> getToken(@RequestBody LoginDto loginDto){
        LoginVo login = userService.loginToken(loginDto);
        return ApiResult.success(login);
    }
    /**
     * 退出登录
     * @return
     */
    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public ApiResult<?> logout(){
        userService.logout();
        return ApiResult.success();
    }
    /**
     * 获取登录信息
     * @return
     */
    @ApiOperation(value = "获取登录人信息")
    @GetMapping("/getInfo")
    public ApiResult<UserInfoVo> getInfo(){
        UserInfoVo userInfoVo = userService.getLoginInfo();
        return ApiResult.success(userInfoVo);
    }
    /**
     * 登录图片验证码
     * @return 图片验证码
     */
    @GetMapping("/captchaImage")
    @ApiOperation(value = "获取图片验证码")
    public ApiResult<CaptchaImageVo> getCode() {
        CaptchaImageVo captchaImageVo = new CaptchaImageVo();
        // 保存验证码信息
        String uuid = IdUtil.simpleUUID();
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 45, 4, 4);

        // 自定义验证码内容为四则运算方式
        MathGenerator mathGenerator = new MathGenerator(1);

        captcha.setGenerator(mathGenerator);

        // 重新生成code
        captcha.createCode();
        String code = captcha.getCode();

        BufferedImage image = captcha.getImage();

        // 转换流信息写出
        FastByteArrayOutputStream os = new FastByteArrayOutputStream();
        try{
            ImageIO.write(image, "png", os);
        }catch (IOException e){
            return ApiResult.error(e.getMessage());
        }
        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
        // 验证码缓存
        redisCache.setCacheObject(verifyKey,code,CacheConstants.CAPTCHA_EXPIRATION,CacheConstants.CAPTCHA_UNIT);
        captchaImageVo.setUuid(uuid);
        captchaImageVo.setCaptchaImg(Base64.encode(os.toByteArray()));
        return ApiResult.success(captchaImageVo);
    }

    @PostMapping("/register")
    public ApiResult<?> registerUser(@RequestBody RegisterUserDto registerUserDto){
        this.userService.addRegisterUser(registerUserDto);
        return ApiResult.success();
    }
}
